﻿Imports System.Runtime.InteropServices

Public Class frmMain

    <DllImport("user32.dll")> _
    Private Shared Function ReleaseCapture() As Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
    End Function

    Private Sub frmMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        End
    End Sub

    Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If (e.CloseReason = CloseReason.UserClosing) AndAlso btnLock.Enabled AndAlso (MsgBox("退出程序将导致计算机失去破解保护，您确定要继续吗？", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation, "警告") = MsgBoxResult.No) Then
            e.Cancel = True
        Else
            If btnLock.Enabled Then btnLock_Click(sender, e)
        End If
    End Sub

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If My.Computer.FileSystem.FileExists(Environ("windir") & "\system32\pcClient.exe") Then BossKey_Keyboard.Checked = True
    End Sub

    Private Sub frmMain_TextChanged(sender As Object, e As System.EventArgs) Handles Me.TextChanged
        MyText.Text = Me.Text
    End Sub

    Private Sub btnUnlock_Click(sender As System.Object, e As System.EventArgs) Handles btnUnlock.Click
        btnUnlock.Enabled = False
        UnlockControl()
        btnLock.Enabled = True
    End Sub

    Private Sub btnLock_Click(sender As System.Object, e As System.EventArgs) Handles btnLock.Click
        btnLock.Enabled = False
        ResumeControl()
        btnUnlock.Enabled = True
    End Sub

    Dim LastState As Boolean = False
    Private Sub BossKey_MouseListener_Tick(sender As System.Object, e As System.EventArgs) Handles BossKey_MouseListener.Tick
        If BossKey_Mouse.Checked Then
            If (GetAsyncKeyState(Keys.LButton) < 0) AndAlso (GetAsyncKeyState(Keys.RButton) < 0) Then
                If LastState Then Exit Sub
                LastState = True
                If btnLock.Enabled Then
                    btnLock.Enabled = False
                    ResumeControl()
                    btnUnlock.Enabled = True
                Else
                    btnUnlock.Enabled = False
                    UnlockControl()
                    btnLock.Enabled = True
                End If
            Else
                LastState = False
            End If
        ElseIf BossKey_Keyboard.Checked Then
            If (GetAsyncKeyState(Keys.ControlKey) < 0) AndAlso (GetAsyncKeyState(Keys.Menu) < 0) AndAlso (GetAsyncKeyState(Keys.Z) < 0) Then
                If LastState Then Exit Sub
                LastState = True
                If btnLock.Enabled Then
                    btnLock.Enabled = False
                    ResumeControl()
                    btnUnlock.Enabled = True
                Else
                    btnUnlock.Enabled = False
                    UnlockControl()
                    btnLock.Enabled = True
                End If
            Else
                LastState = False
            End If
        End If
    End Sub

    Private Sub btnAbout_Click(sender As System.Object, e As System.EventArgs) Handles btnAbout.Click
        Me.Hide()
        frmAbout.Show()
    End Sub

    Private Sub btnHide_Click(sender As System.Object, e As System.EventArgs) Handles btnHide.Click
        Me.Hide()
        MainIcon.ShowBalloonTip(1500, "广雅破解套装", "程序继续后台运行中……", ToolTipIcon.Info)
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub CustomTitleBar_MouseDown() Handles CustomTitleBar.MouseDown, MyText.MouseDown
        ReleaseCapture()
        SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End Sub

    Private Sub MainIcon_Click(sender As Object, e As EventArgs) Handles MainIcon.Click
        Me.Show()
    End Sub

    Private Sub BrowserRefresh_Tick(sender As Object, e As EventArgs) Handles BrowserRefresh.Tick
        Browser.Refresh(WebBrowserRefreshOption.Completely)
    End Sub

    Public Shared Sub Restart()
        Application.Restart()
    End Sub

End Class
